﻿using ChinaBlock;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Box_games
{
    public partial class single : Form
    {
        public static Login f4;
        private Block currentBlock; //当前在运行的方块
        private Block nextBlock;   //下一个即将出现的方块
        private Point startLocation = new Point(GameField.SquareSize * 8, 0);  //方块产生的位置
        private int score = 0;            //玩家积分
        private bool stillRuning = true; //游戏运行开关
        public static string name;//玩家名称
        public single()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            name = f4.textBox1.Text;
            GameField.BackColor = picBackGround.BackColor;
            GameField.winHandle = picBackGround.Handle;
            timer1.Interval = 1000;
            label3.Enabled = false;
            //获取自定义设置
            getSettings();
        }
        private void getSettings()
        {
            if (!File.Exists("Setting.ini"))
                return;
            FileStream fs = new FileStream("Setting.ini", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            string line1 = sr.ReadLine();
            string line2 = sr.ReadLine();
            string line3 = sr.ReadLine();
            if (line1 != null && line1.Split('=').Length > 1)
            {
                GameField.BackColor = Color.FromArgb(int.Parse(line1.Split('=')[1]));
                picBackGround.BackColor = GameField.BackColor;
            }
            if (line2 != null && line2.Split('=').Length > 1)
                GameField.BlockForeColor = strToColor(line2.Split('=')[1]);
            if (line3 != null && line3.Split('=').Length > 1)
                GameField.BlockBackColor = strToColor(line3.Split('=')[1]);
            sr.Close();
            fs.Close();
        }

        /*颜色数组*/
        private Color[] strToColor(string str)
        {
            string[] strs = str.Split(',');
            if ((strs.Length - 1) != 7)
                return null;
            Color[] colors = new Color[7];
            for (int i = 0; i < strs.Length - 1; i++)
                colors[i] = Color.FromArgb(int.Parse(strs[i]));
            return colors;
        }




        private void timer1_Tick(object sender, EventArgs e)
        {

            if (!stillRuning)
                return;

            //检测是否还可以下移
            if (!currentBlock.down())
            {
                if (currentBlock.Top() == 0)
                {//如果到顶则游戏结束
                    showMsg("游戏结束");
                    //结束时分数传入数据库
                    int num = score;
                    string sql = string.Format($"insert into Dan(name,score) values('{name}','{num}')");
                    DBHelpe.ADD(sql);
                    stillRuning = false;
                    timer1.Stop();
                   
                    return;
                }
                //否则计算分数并继续
                int eraseLines = GameField.CheckLines();
                if (eraseLines > 0)
                {
                    score += GameField.width * eraseLines;
                    t_score.Text = score.ToString();
                    picBackGround.Invalidate();
                    Application.DoEvents();
                    GameField.Redraw();
                }
                //产生下一个block
                currentBlock = new Block(startLocation, nextBlock.blockType);
                currentBlock.Draw(GameField.winHandle);
                pic_preView.Refresh();
                nextBlock = new Block(new Point(80, 50), Block.BlockTypes.undefined);
                nextBlock.Draw(pic_preView.Handle);
            }
            currentBlock.down();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            beginGame();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            showMsg("暂 停");
            label3.Enabled = true;
            label4.Enabled = false;
        }

        private void Form2_Activated(object sender, EventArgs e)
        {

            picBackGround.Invalidate();
            Application.DoEvents();
            GameField.Redraw();
        }
        private void beginGame()
        {
            msg.SendToBack();   //将提示窗口隐藏
            label3.Enabled = false;
            label4.Enabled = true;

            if (currentBlock == null)
            {//第一次开始
                currentBlock = new Block(startLocation, Block.BlockTypes.undefined);
                currentBlock.Draw(GameField.winHandle);
                nextBlock = new Block(new Point(80, 50), Block.BlockTypes.undefined);
                nextBlock.Draw(pic_preView.Handle);
                stillRuning = true;
                timer1.Start();
            }
            else
            {
                timer1.Enabled = true;
            }
        }
        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Right: currentBlock.right(); break;//向右移动
                case Keys.Left: currentBlock.left(); break; //向左移动
                case Keys.Up: currentBlock.Rotate(); break; //旋转
                case Keys.Down: while (currentBlock.down()) ; break; //向下加速
                case Keys.Space:                           //空格：暂停
                    timer1.Enabled = !timer1.Enabled;
                    if (!timer1.Enabled)
                        showMsg("暂 停");
                    else
                        msg.SendToBack();
                    break;
                case Keys.Enter: //回车继续游戏
                    beginGame();
                    break;
            }
   
        }
        private void showMsg(string str)
        {
            msg.Text = str;
            msg.Left = picBackGround.Left + picBackGround.Width / 2 - msg.Width / 2;
            msg.BringToFront();
        }

        private void label7_Click(object sender, EventArgs e)
        {
            stillRuning = false;
            timer1.Stop();
            currentBlock = null;
            picBackGround.Refresh();
            pic_preView.Refresh();
            Main f3 = new Main();
            GameField.arriveBlock = new Square[GameField.width, GameField.height]; //清空所有小方块
            GameField.arrBitBlock = new int[GameField.height];
            f3.Show();
            this.Hide();
        }

        private void label8_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            picBackGround.Refresh();   //刷新游戏区
            pic_preView.Refresh();     //刷新预览区
            GameField.arriveBlock = new Square[GameField.width, GameField.height]; //清空所有小方块
            GameField.arrBitBlock = new int[GameField.height];
            score = 0;           //重新计算积分
            t_score.Text = "0";
            msg.SendToBack();   //将提示窗口隐藏
            currentBlock = new Block(startLocation, Block.BlockTypes.undefined);
            currentBlock.Draw(GameField.winHandle);
            nextBlock = new Block(new Point(80, 50), Block.BlockTypes.undefined);
            nextBlock.Draw(pic_preView.Handle);
            label3.Enabled = false;
            label4.Enabled = true;
            stillRuning = true;
            timer1.Start();
        }

    }
}
